package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartPanel;

import ui.components.Chart;

public class MainPanel extends JFrame {

	private JPanel contentPane;
	private JTextField txtNroMesa;
	private JTextField txtCantVotos;
	private JTable tablaVotos;
	private MainView mview;
	private JTextField txtBarrio;
	private java.util.Vector<Chart> metricsPanels;
	private JTabbedPane metricsPane;
	/**
	 * Create the frame.
	 * @param mainView 
	 * @param runnable 
	 */
	public MainPanel(MainView mainView) {
		this.setTitle("Election Manager v1.0");
		this.mview = mainView;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 798, 606);
		this.setResizable(false);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnArchivo = new JMenu("Archivo");
		menuBar.add(mnArchivo);
		
		JMenuItem mntmSalir = new JMenuItem("Salir");
		mntmSalir.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				System.exit(1);
			}
		});
		mnArchivo.add(mntmSalir);
		
		JMenu mnMetrica = new JMenu("Metricas");
		menuBar.add(mnMetrica);
		
		JMenuItem mntmAgregarMetrica = new JMenuItem("Agregar Metrica");
		mnMetrica.add(mntmAgregarMetrica);
		mntmAgregarMetrica.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				mview.showMetricGen();
			}
		});
		
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(5, 441, 779, 120);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel lblSexo = new JLabel("Sexo:");
		lblSexo.setBounds(10, 11, 44, 14);
		panel.add(lblSexo);

		final JRadioButton btnMasc = new JRadioButton("Masculino");
		btnMasc.setSelected(true);

		btnMasc.setBounds(60, 7, 90, 23);
		panel.add(btnMasc);

		final JRadioButton btnFem = new JRadioButton("Femenino");

		btnFem.setBounds(152, 7, 88, 23);
		panel.add(btnFem);

		JLabel lblNroMesa = new JLabel("Nro mesa:");
		lblNroMesa.setBounds(10, 36, 82, 14);
		panel.add(lblNroMesa);

		txtNroMesa = new JTextField();
		txtNroMesa.setBounds(77, 31, 73, 20);
		panel.add(txtNroMesa);
		txtNroMesa.setColumns(10);
		
		final JComboBox cBPtdo = new JComboBox();
		cBPtdo.setBounds(77, 59, 247, 20);
		panel.add(cBPtdo);
		
		JLabel lblPartido = new JLabel("Partido:");
		lblPartido.setBounds(10, 62, 46, 14);
		panel.add(lblPartido);
		
		JLabel lblCantVotos = new JLabel("Cant Votos:");
		lblCantVotos.setBounds(8, 87, 62, 14);
		panel.add(lblCantVotos);
		
		txtCantVotos = new JTextField();
		txtCantVotos.setBounds(77, 84, 144, 20);
		panel.add(txtCantVotos);
		txtCantVotos.setColumns(10);
		final DefaultTableModel tableModel = new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"Partido", "Cantidad de Votos"
				}
			){
			Class[] columnTypes = new Class[] {
					String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			};
		
		cBPtdo.addItem("Obrero");
		cBPtdo.addItem("FPV");
		cBPtdo.addItem("Radical");
		cBPtdo.addItem("PSur");
		final JButton btnSubmit = new JButton("Enviar");
		
		btnSubmit.setEnabled(false);
		btnSubmit.setBounds(680, 83, 89, 23);
		panel.add(btnSubmit);
		
		JButton btnAgregar = new JButton("Agregar");
		
		btnAgregar.setBounds(235, 83, 89, 23);
		panel.add(btnAgregar);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(334, 8, 336, 93);
		panel.add(scrollPane);
		
		tablaVotos = new JTable();
		scrollPane.setViewportView(tablaVotos);
		tablaVotos.setModel(tableModel);
		tablaVotos.getColumnModel().getColumn(0).setPreferredWidth(121);
		tablaVotos.getColumnModel().getColumn(1).setPreferredWidth(125);
		
		tablaVotos.setRowSelectionAllowed(false);
		
		JLabel lblBarrio = new JLabel("Barrio:");
		lblBarrio.setBounds(175, 36, 46, 14);
		panel.add(lblBarrio);
		
		txtBarrio = new JTextField();
		txtBarrio.setBounds(234, 31, 90, 20);
		panel.add(txtBarrio);
		txtBarrio.setColumns(10);
		
		final JTabbedPane metricsPane = new JTabbedPane(JTabbedPane.TOP);
		metricsPane.setBounds(5, 0, 779, 430);
		contentPane.add(metricsPane);
		JMenuItem mntmActualizar = new JMenuItem("Actualizar");		
		mntmActualizar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				metricsPanels = mview.updateMetrics();
				metricsPane.removeAll();
				if(metricsPanels == null)
					System.out.println("No hay metricas para mostrar");
				else
					for(Chart cP: metricsPanels){
						metricsPane.addTab(" "+ cP.getChart().getName(), null, cP.getChart() , null);
					}	
			}
		});
		mnMetrica.add(mntmActualizar);

		btnMasc.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				btnFem.setSelected(false);
			}
		});
		btnFem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				btnMasc.setSelected(false);
			}
		});
		
		btnAgregar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tableModel.addRow(new Object[]{cBPtdo.getSelectedItem(), txtCantVotos.getText()});
				btnSubmit.setEnabled(true);
			}
		});

		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean storeStatus = false;
				if(!btnFem.isSelected() && !btnMasc.isSelected()){
					System.out.println("Debe seleccionar un sexo");
				}else{
					storeStatus = mview.store(tableModel.getDataVector(),txtNroMesa.getText(),btnMasc.isSelected(),txtBarrio.getText());										
				}
				//Se resetea la interfaz grafica
				while(tableModel.getRowCount()>0){
					tableModel.removeRow(0);
				}
				
				txtNroMesa.setText("");
				txtBarrio.setText("");
				txtCantVotos.setText("");			
				
				metricsPanels = mview.updateMetrics();
				metricsPane.removeAll();
				if(metricsPanels == null)
					System.out.println("No hay metricas para mostrar");
				else
					for(Chart cP: metricsPanels){
						metricsPane.addTab(" "+ cP.getChart().getName(), null, cP.getChart() , null);
					}				
							
			}
						
		});					
	
	}	
		
}
